package mypackage;

import java.util.Vector;

import mypackage.dao.DBObject;
import mypackage.dao.ListDao;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.util.Comparator;

/**
 * This class extends the UiApplication class, providing a graphical user
 * interface.
 */
public class HelloBlackBerry extends UiApplication {

	/**
	 * Entry point for application
	 * 
	 * @param args
	 *            Command line arguments (not used)
	 */

	static public DBObject dbObject;
	static PersistentObject persistentObject;
	public ListScreen listScreen, listItemsScreen;

	public static void main(String[] args) {
		// Create a new instance of the application and make the currently
		// running thread the application's event dispatch thread.
		HelloBlackBerry theApp = new HelloBlackBerry();
		theApp.enterEventDispatcher();

	}

	/**
	 * Creates a new HelloBlackBerry object
	 */
	public HelloBlackBerry() {
		// Push a screen onto the UI stack for rendering.
		listScreen = new ListScreen(null);
		HomeScreen homeScreen=new HomeScreen(this);
		pushScreen(homeScreen);
	}

	public void openItemsScreen(Object o) {
		CheckboxListScreen checkboxListScreen=new CheckboxListScreen((ListDao) o);
		pushScreen(checkboxListScreen);
	}
	
	public void appendToList(String s) {
		listScreen.addItem(s);
	}

	public void appendListItem(String itemName) {
		listItemsScreen.addItem(itemName);
	}

	public int selectedIndex = -1;

//	public void openListWithIndex(int index) {
//		selectedIndex = index;
//		listItemsScreen = new ListScreen(dbObject.getListDao(index));
//		pushScreen(listItemsScreen);
//	}
//
//	public void deleteList(int index) {
//		dbObject.deleteList(index);
//	}
//
//	public void deleteListItem(int index1) {
//		dbObject.deleteItemInList(selectedIndex, index1);
//	}

	public void deactivate() {
		synchronized (persistentObject) {
			persistentObject.setContents(dbObject);
			persistentObject.commit();
		}
		super.deactivate();
	}

	static {
		persistentObject = PersistentStore
				.getPersistentObject(0xdec6a67096f836cL);
		synchronized (persistentObject) {
			if (persistentObject.getContents() == null) {
				persistentObject.setContents(new DBObject());
				persistentObject.commit();
			}
		}
		dbObject = new DBObject();
		dbObject = (DBObject) persistentObject.getContents();
	}
	
}
